package com.dragon.www.haier.download;

import android.os.Environment;

import com.dragon.www.haier.SHApplication;
import com.dragon.www.haier.net.Netroid;
import com.dragon.www.haier.util.Utils;
import com.duowan.mobile.netroid.Listener;
import com.duowan.mobile.netroid.RequestQueue;
import com.duowan.mobile.netroid.request.FileDownloadRequest;
import com.duowan.mobile.netroid.toolbox.FileDownloader;

import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;


/**
 * Created by eajon on 2015/7/9.
 */
public class DownloadManager {

    private static FileDownloader mDownloder;
    public static final String mSaveDirPath = Environment.getExternalStorageDirectory() + "/smarthome/";
    public static final String mSaveLogoDirPath = Environment.getExternalStorageDirectory() + "/smarthome/logo/";

    public static final String mSaveLogoFile = mSaveLogoDirPath + "logo.jpg";
    private static String downloadBaseUri = "http://www.csufo.com:8001/Public/res/";
    private static String downloadSoundUri = downloadBaseUri + "adhan/";


    public static final int LOGO = 1;

    private static DownloadManager self = null;

    public DownloadManager() {
        init();
    }

    public static DownloadManager getInstance() {
        if (null == self) {
            self = new DownloadManager();
        }
        return self;
    }

    private void init() {
        RequestQueue queue = Netroid.newRequestQueue(SHApplication.getInstance(), null);
        mDownloder = new FileDownloader(queue, 1) {
            @Override
            public FileDownloadRequest buildRequest(String storeFilePath, String url) {
                return new FileDownloadRequest(storeFilePath, url) {
                    @Override
                    public void prepare() {
                        addHeader("Accept-Encoding", "identity");
                        super.prepare();
                    }
                };
            }
        };

        createFile();
    }

    private static void createFile() {
        File downloadDir = new File(mSaveDirPath);
        if (!downloadDir.exists()) downloadDir.mkdir();
        File downloadLogoDir = new File(mSaveLogoDirPath);
        if (!downloadLogoDir.exists()) downloadLogoDir.mkdir();
        File pictureDir = new File(Environment.getExternalStorageDirectory() + "/Pictures/");
        if (!pictureDir.exists()) pictureDir.mkdir();
    }

    public static FileDownloader.DownloadController download(int type, String fileName, Listener<Void> listener) {
        FileDownloader.DownloadController controller = null;
        if (!Utils.sdacrdExist()) {
            return controller;
        }
        createFile();
        switch (type) {
            case LOGO:
                controller = mDownloder.add(mSaveLogoFile, fileName, listener);
                break;
        }
        return controller;
    }

    public void clearAllDownloadTask()
    {
        mDownloder.clearAll();
    }

    public static void makeDir(String directorypath) {
        File f = new File(directorypath);
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
    }

    public static String getFileName(String urlString) {
        String file = getTimeString() + ".jpg";
        return file;
    }

    private static String getTimeString() {
        Calendar calendar = new GregorianCalendar();
        StringBuffer sb = new StringBuffer();
        sb.append(String.valueOf(calendar.get(Calendar.YEAR)))
                .append(valueOfString(String.valueOf(calendar.get(Calendar.MONTH) + 1), 2))
                .append(valueOfString(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)), 2))
                .append(valueOfString(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), 2))
                .append(valueOfString(String.valueOf(calendar.get(Calendar.MINUTE)), 2))
                .append(valueOfString(String.valueOf(calendar.get(Calendar.SECOND)), 2))
                .append(valueOfString(String.valueOf(calendar.get(Calendar.MILLISECOND)), 3));
        return sb.toString();
    }

    private static  String valueOfString(String str, int len) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len - str.length(); i++) {
            sb.append("0");
        }
        return (sb.length() == 0) ? (str) : (sb.toString() + str);
    }
}
